package com.czwj.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.Attendance;
import com.czwj.entities.OperateResult;
import com.czwj.entities.UserInfo;
import com.czwj.service.AttendanceService;

@Controller
public class AttendanceAction{
	@Resource
	AttendanceService attendanceService;
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	@RequestMapping(value = "/existattendance",method = RequestMethod.GET)
	@ResponseBody
	public List<String> existattendance(@RequestParam(value = "projectitemid") int projectitemid,
			@RequestParam(value = "yearmonth") String yearmonth){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("projectitemid",projectitemid);
		map.put("yearmonth",yearmonth);
		List<String> list = attendanceService.existattendance(map);
		return list;
	}
	
	@RequestMapping(value = "/attendance/findoneday",method = RequestMethod.GET)
	@ResponseBody
	public Attendance findoneday(@RequestParam("projectitemid") int projectitemid,@RequestParam("date") Date date){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("projectitemid",projectitemid);
		map.put("date",sdf.format(date));
		Attendance attendance = attendanceService.findonday(map);
		if(attendance == null){
			attendance = new Attendance();
		}
		return attendance;
	}
	
	@SystemControllerLog(description = "考勤")
	@RequestMapping(value = "/attendanceSave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult attendanceSave(@RequestParam(value = "duty",required = true) int[] duty,
			@RequestParam(value = "projectitemid",required = true) Integer projectitemid,
			@RequestParam(value = "projectitemchildid",required = false) Integer projectitemchildid,
			@RequestParam(value = "date",required = true) String date,
			@RequestParam(value = "images",required = true) MultipartFile[] images,HttpServletRequest request)
			throws Exception{
		OperateResult result = new OperateResult();
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = simpleDateFormat.parse(date);
		Attendance attendance = new Attendance(projectitemid,projectitemchildid,date1,userInfo.getId(),new Date(),
				request.getRemoteAddr());
		int cnt = attendanceService.insert(attendance,images,duty);
		result.setSuccess(cnt > 0);
		return result;
	}
}
